package com.xinsoft.config.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @Author Li9527
 * @Description MQTT工具类操作
 * @Date 2023/4/12 16:13
 **/
@Slf4j
@Component
@Service
public class MQTTConnect {
    // 发送地址：苏北粮油
//    private String HOST = "tcp://36.133.139.147:1883"; //mqtt服务器的地址和端口
//    private final String clientId = "13822fb2-aeba-4041-ac1f-8cf1b497d602";

    // 接收地址：用新材
    private String HOST = "tcp://143.64.38.146:1883";
    private final String clientId = "7cc1280b-ce96-41ea-be06-f1136b09905a";

//    private String HOST = "tcp://36.133.91.6:1883";
//    private final String clientId = "7cc1280b-ce96-41ea-be06-f1136b09905a";


    // 发送地址：本地
//    private String HOST = "tcp://192.168.1.80:1883";
//    private final String clientId = "2f41e947-7507-424b-a676-38eb7a1f671d";
    private MqttClient mqttClient;

    /**
     * 客户端connect连接mqtt服务器
     *
     * @param userName     用户名
     * @param passWord     密码
     * @param mqttCallback 回调函数
     **/
    public void setMqttClient(String userName, String passWord, MqttCallback mqttCallback) throws MqttException {
        MqttConnectOptions options = mqttConnectOptions(userName, passWord);
        if (mqttCallback == null) {
            mqttClient.setCallback(new Callback());
        } else {
            mqttClient.setCallback(mqttCallback);
        }
        mqttClient.connect(options);
    }

    /**
     * MQTT连接参数设置
     */
    private MqttConnectOptions mqttConnectOptions(String userName, String passWord) throws MqttException {
        mqttClient = new MqttClient(HOST, clientId, new MemoryPersistence());
        MqttConnectOptions options = new MqttConnectOptions();
//        options.setUserName(userName);
//        options.setPassword(passWord.toCharArray());
        options.setConnectionTimeout(100);///默认：30
        options.setAutomaticReconnect(false);//默认：false
        options.setCleanSession(true);//默认：true
        //options.setKeepAliveInterval(20);//默认：60
        return options;
    }

    /**
     * 关闭MQTT连接
     */
    public void close() throws MqttException {
        mqttClient.close();
        mqttClient.disconnect();
    }

    /**
     * 向某个主题发布消息 默认qos：1
     *
     * @param topic:发布的主题
     * @param msg：发布的消息
     */
    public void pub(String topic, String msg) throws MqttException {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(0);
        mqttMessage.setPayload(msg.getBytes());
        MqttTopic mqttTopic = mqttClient.getTopic(topic);
        MqttDeliveryToken token = mqttTopic.publish(mqttMessage);
        token.waitForCompletion();
    }

    /**
     * 向某个主题发布消息
     *
     * @param topic: 发布的主题
     * @param msg:   发布的消息
     * @param qos:   消息质量    Qos：0、1、2
     */
    public void pub(String topic, String msg, int qos) throws MqttException {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(qos);
        mqttMessage.setPayload(msg.getBytes());
        MqttTopic mqttTopic = mqttClient.getTopic(topic);
        MqttDeliveryToken token = mqttTopic.publish(mqttMessage);
        token.waitForCompletion();
    }

    /**
     * 订阅某一个主题 ，此方法默认的的Qos等级为：1
     *
     * @param topic 主题
     */
    public void sub(String topic) throws MqttException {
        mqttClient.subscribe(topic);
    }

    /**
     * 订阅某一个主题，可携带Qos
     *
     * @param topic 所要订阅的主题
     * @param qos   消息质量：0、1、2
     */
    public void sub(String topic, int qos) throws MqttException {
        mqttClient.subscribe(topic, qos);
    }

    /**
     * main函数自己测试用
     */
    public static void main(String[] args) throws MqttException {
        MQTTConnect mqttConnect = new MQTTConnect();
        mqttConnect.setMqttClient("", "", new Callback());
        mqttConnect.sub("v1/devices/me/telemetry",0);
//        mqttConnect.pub("/Li9527", "Mr.Li" + (int) (Math.random() * 100000000));
//        mqttConnect.close();
    }
}